package algo;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import data.Mail;

public class SimilarityMeanNearNeighborsEvaluator implements NearNeighborsEvaluator
{
	private static final SimilarityMeanNearNeighborsEvaluator instance =  new SimilarityMeanNearNeighborsEvaluator();
	
	private SimilarityMeanNearNeighborsEvaluator()
	{
		
	}
	
	public static SimilarityMeanNearNeighborsEvaluator getInstance()
	{
		return instance;
	}
	
	@Override
	public double evaluate(Collection<? extends Entry<Double, Mail>> simiarities)
	{
		double evaluation;
		double spamSimilaritySum = 0;
		double nspamSimilaritySum = 0;
		for (Map.Entry<Double, Mail> entry : simiarities)
		{
			double similarity = entry.getKey();
			Mail mail = entry.getValue();
			
			if (mail.isSpam())
			{
				spamSimilaritySum += similarity;
			}
			else
			{
				nspamSimilaritySum += similarity;
			}
		}
		evaluation = (1 + (spamSimilaritySum - nspamSimilaritySum) / simiarities.size()) / 2;
		return evaluation;
	}

}
